package com.msmall.base.rms.controller;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.msmall.base.PaginationSupport;
import com.msmall.base.PaginationSupportUtil;
import com.msmall.base.ReturnBase;
import com.msmall.base.rms.manager.RMSManager;
import com.msmall.base.rms.po.Administer;
import com.msmall.base.rms.po.Group;
import com.msmall.base.rms.util.RMSUtils;
import com.msmall.erp.base.Constant;

@Controller
public class AdministerController {
private static final Logger log = Logger.getLogger(AdministerController.class);
	
	@Autowired
	private RMSManager rmsManager;


	@RequestMapping("/rms/administer/index")
	public String index(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
		try{
			return "rms.administer.index";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	
//	/**
//	 * 获取所有的组信息，以及已经获得授权的组
//	 * @param request
//	 * @param response
//	 */
//	@RequestMapping("/rms/administer/getAuthGroup")
//	public void getAuthGroup(HttpServletRequest request, HttpServletResponse response) {
//		try{
//			HttpSession session = request.getSession();
//			// 从session 里面获取用户名的信息
//			AdministerVO vo = (AdministerVO) session.getAttribute(LoginConstant.HTTP_SESSION_KEY_ADMINISTER);
//			response.setContentType("text/plain;charset=UTF-8");
//			PrintWriter out = response.getWriter();
//			JSONObject json = rmsManager.getModuleByAdministerJson(vo.getId());
//			if(json != null){
//				out.print(json.toString());
//			}else{
//				log.info(" =====> NULL ");
//				out.print("");
//			}
//		}catch(Exception e){
//			log.error(e);
//		}
//	}
	
	@RequestMapping("/rms/administer/search")
	public String search(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, Map<String, Object> model) {
		try{
			PaginationSupport page = PaginationSupportUtil.get(request);
			model.put("page", page);
			rmsManager.searchAdminister(username, page);
			return "rms.administer.search.result";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 进入创建/更新实体页面
	 */
	@RequestMapping("/rms/administer/entity")
	public String entity(HttpServletRequest request, HttpServletResponse response, @RequestParam Integer administerid, Map<String, Object> model) {
		try{
			if(administerid != null && administerid != 0){
				Administer administer = rmsManager.getAdminister(administerid);
				model.put("entity", administer);
				
				List<Group> all = rmsManager.getAllGroup();
				List<Group> auth = rmsManager.getGroupByAdminister(administerid);
				model.put("allGroups", all);
				model.put("authGroups", auth);
			}
			return "rms.administer.entity";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 保存创建/更新的实体
	 */
	@RequestMapping("/rms/administer/save")
	public String save(HttpServletRequest request, HttpServletResponse response, Administer administer, Map<String, Object> model) {
		try{
			rmsManager.saveAdminister(administer);
			return "rms.administer.index";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 给管理员付权。每次在页面传递一个组ID
	 * action=1,增加权限;action=-1取消权限 
	 * RMSUtils.AUTH_ACTION_*
	 */
	@RequestMapping("/rms/administer/auth")
	public void auth(HttpServletRequest request, HttpServletResponse response, Integer administerid, Integer groupid, String action, Map<String, Object> model){
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			rmsManager.authAdministerGroup(administerid, groupid, action);
			out = response.getWriter();
			json.put( ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_SUCCESS);
		}catch(Exception e){
			log.error(e);
			json.put(ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_FAILED);
		}
		out.print(json.toString());
	}
	/**
	 * 删除模块
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/rms/administer/remove")
	public void remove(HttpServletRequest request, HttpServletResponse response,@RequestParam Integer administerid){
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			out = response.getWriter();
			rmsManager.removeAdminister(administerid);
			json.put( ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_SUCCESS);
		}catch(Exception e){
			log.error(e);
			json.put(ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_FAILED);
		}
		out.print(json.toString());
	}
	
	/**
	 * 进入修改密码页面
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/rms/administer/modifyPassword")
	public String modifyPassword(HttpServletRequest request, HttpServletResponse response,@RequestParam Integer administerid, Map<String, Object> model){
		try{
			Administer administer = rmsManager.getAdminister(administerid);
			model.put("entity", administer);
			return "rms.administer.modifypassword";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	
	/**
	 * 修改密码
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/rms/administer/savePassword")
	public void savePassword(HttpServletRequest request, HttpServletResponse response,@RequestParam Administer administer){
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			out = response.getWriter();
			rmsManager.modifyPassword(administer);
			json.put( ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_SUCCESS);
		}catch(Exception e){
			log.error(e);
			json.put(ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_FAILED);
		}
		out.print(json.toString());
	}
}
